﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.Core;


namespace ERP.Manager.BusinessServices
{
    public class ContextService
    {
            private static ContextService instance;
            private readonly ISessionProvider sessionProvider;

            public ContextService()
            {
            }

            public ContextService(ISessionProvider sessionProvider)
            {
                this.sessionProvider = sessionProvider;
            }

            public static ContextService Instance()
            {
                if (instance == null)
                    instance = new ContextService(new DefaultSessionProvider());

                return instance;
            }

            public static ContextService Current
            {
                get { return Instance(); }
                set { instance = value; }
            }

            public void Close()
            {
                sessionProvider.RemoveAll();
                sessionProvider.Abandon();
            }



            public virtual int Id
            {
                get
                {
                    object id = sessionProvider.Get("Id");
                    return id == null ? 0 : (int)id;
                }
                set { sessionProvider.Set("Id", value); }
            }


            public virtual string UserId 
            {
                get { return (string)sessionProvider.Get("UserId"); }
                set { sessionProvider.Set("UserId", value); }
            }

            public virtual string UserName 
            {
                get { return (string)sessionProvider.Get("UserName"); }
                set { sessionProvider.Set("UserName", value); }
            }


            public virtual string DepartmentId
            {
                get { return (string)sessionProvider.Get("DepartmentId"); }
                set { sessionProvider.Set("DepartmentId", value); }
            }

        }

    }
